﻿@inject IDialogService DialogService
@inject IDataConfigService DataConfigService

<MudTable @ref="_requestTable" ServerData="@(new Func<TableState, Task<TableData<ConfigRuleInfoResponseDto>>>(ServerReload))"
          MultiSelection="true" Hover="true" Dense="true" Breakpoint="Breakpoint.Xs" Elevation="0"
          ReadOnly="true" FixedHeader="true" CustomFooter="true" HorizontalScrollbar="true">
    <ToolBarContent>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="@(_ => AddOrUpdateHandler())">新增规则</MudButton>
        <MudSpacer></MudSpacer>
    </ToolBarContent>
    <Columns>
        <SelectColumn T="ConfigRuleInfoResponseDto"/>
        <MudColumn Value="@context.RuleCode" HeaderText="规则编码"/>
        <MudColumn Value="@context.RuleName" HeaderText="规则名称"/>
        <MudColumn Value="@context.RuleType" HeaderText="规则类型"/>
        <MudColumn Value="@context.Sort" HeaderText="排序"/>
        <MudTemplateColumn T="ConfigRuleInfoResponseDto">
            <Header Context="_">
                <MudTh>操作</MudTh>
            </Header>
            <Row Context="_">
                <MudTd>
                    <MudButton StartIcon="@Icons.Material.Filled.Edit" Size="Size.Small" OnClick="@(() => AddOrUpdateHandler(context.Key))">修改</MudButton>
                    <MudButton StartIcon="@Icons.Material.Filled.Remove" Size="Size.Small">删除</MudButton>
                </MudTd>
            </Row>
        </MudTemplateColumn>
    </Columns>
    <PagerContent>
        <MudTablePager/>
    </PagerContent>
</MudTable>


@code {
    private MudTable<ConfigRuleInfoResponseDto> _requestTable = new();
    private ConfigRuleSearchRequestDto _searchRequestDto = new();

    [CascadingParameter]
    public DataConfigIndex? DataConfigIndex { get; set; }


    private async Task<TableData<ConfigRuleInfoResponseDto>> ServerReload(TableState state)
    {
        var dataConfigCode = DataConfigIndex?.GetDataConfigCode();

        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var result = await DataConfigService.QueryDataConfigRulesByPage(dataConfigCode!, _searchRequestDto);

        var totalCount = 0;
        IList<ConfigRuleInfoResponseDto> tableData = new List<ConfigRuleInfoResponseDto>();

        if (result.Succeeded)
        {
            tableData = result.Data;
            totalCount = result.TotalCount;
        }

        return new TableData<ConfigRuleInfoResponseDto>() { TotalItems = totalCount, Items = tableData };
    }

    async Task AddOrUpdateHandler(Guid? id = null)
    {
        var dataConfigCode = DataConfigIndex?.GetDataConfigCode();

        var editData = new ConfigRuleEditRequestDto();
        var title = "新增规则";

        if (id != null)
        {
            title = "修改规则";
            var result = await DataConfigService.GetDataConfigRule(id.Value);
        }

        await DialogService.ShowAsync<DataConfigRuleEdit>(title,
            new DialogParameters
            {
                ["Key"] = id,
                ["EditData"] = editData
            },
            new DialogOptions
            {
                DisableBackdropClick = true,
                MaxWidth = MaxWidth.Medium,
                FullWidth = true
            });
    }

    async Task DeleteHandler(Guid id)
    {
        await DialogService.ShowMessageBox(
            "提示",
            "确定删除选中的用户?",
            yesText: "确定", cancelText: "取消");
    }

}